package com.bootdo.blog.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.blog.domain.HarvestDO;
import com.bootdo.blog.service.HarvestService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;




/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2021-03-07 10:38:31
 */
@Controller
@RequestMapping("/blog/harvest")
public class HarvestController {
	@Autowired
	private HarvestService harvestService;
	
	@GetMapping()
	@RequiresPermissions("blog:harvest:harvest")
	String Harvest(){
	    return "blog/harvest/harvest";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("blog:harvest:list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<HarvestDO> harvestList = harvestService.list(query);
		int total = harvestService.count(query);
		PageUtils pageUtils = new PageUtils(harvestList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("blog:harvest:save")
	String add(){
	    return "blog/harvest/add";
	}
	@GetMapping("/edit/{id}")
	@RequiresPermissions("blog:harvest:update")
	String edit(@PathVariable("id") Integer harvestId, Model model){
		HarvestDO harvestDO = harvestService.get(harvestId);
		model.addAttribute("harvestDO", harvestDO);
	    return "blog/harvest/edit";
	}
	/**
	 * 信息
	 */
	@RequestMapping("/info/{harvestId}")
	@RequiresPermissions("blog:harvest:info")
	public R info(@PathVariable("harvestId") Integer harvestId){
		HarvestDO harvest = harvestService.get(harvestId);
		return R.ok().put("harvest", harvest);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("blog:harvest:save")
	public R save( HarvestDO harvest){
		if(harvestService.save(harvest)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("blog:harvest:update")
	public R update( HarvestDO harvest){
		harvestService.update(harvest);
		
		return R.ok();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("blog:harvest:remove")
	public R remove( @RequestParam("id")Integer harvestId){
		if(harvestService.remove(harvestId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("blog:harvest:remove")
	public R remove(@RequestParam("ids[]") Integer[] harvestIds){
		harvestService.batchRemove(harvestIds);
		
		return R.ok();
	}
	
}
